//
//  MainPageViewController.m
//  UcarTest
//
//  Created by 陈曦 on 16/1/13.
//  Copyright © 2016年 chenxi. All rights reserved.
//

#import "MainPageViewController.h"
#import "Define.h" 
#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "DIYChoiceCarTableViewCell.h"
#import "CarDetailsViewController.h"
#import "DIYDatePickerView.h"
#import "PersonalInformationDetailViewController.h"
#import "AFNetworking.h"
#import "NetRequestGroup.h" 
#import "CarListMianPageModel.h"
#import "Userinfo.h"
#import "TenantCertificationViewController.h"
#import "MBProgressHUD.h"
#import "LongLetCarViewController.h"
#import "RenterOrdersViewController.h"
#import "CarOwnerOrdersViewController.h"



@interface MainPageViewController ()

@property (nonatomic, assign) CGFloat scalef;//实时横向位移

@property (nonatomic, strong) NSMutableArray *listArray;//列表返回值集合

@property (nonatomic, assign) NSInteger pageValue;//返回系统参数

@property (nonatomic, assign) NSInteger allPage;//总页数

@property (nonatomic, strong) DIYChoiceCarTableViewCell *cell;

@end

@implementation MainPageViewController

static NSString *reidentifier = @"cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //隐藏导航条
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"联合出行";
    self.navigationItem.titleView = titleLabel;
    self.navigationController.navigationBar.barTintColor = mainColor;
    self.navigationController.navigationBarHidden = YES;
    
    //重定义返回键
    UIButton *headBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    headBtn.frame = CGRectMake(10, 24, 30, 30);
    [headBtn setImage:[UIImage imageNamed:@"mian-head-Img"] forState:UIControlStateNormal];
    [headBtn addTarget:self action:@selector(openLeftListError) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBar = [[UIBarButtonItem alloc] initWithCustomView:headBtn];
    self.navigationItem.leftBarButtonItem = backBar;
    

    
    //蒙板
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.contentView.alpha = 0.4;
    self.contentView.backgroundColor = [UIColor blackColor];
    
    //设置背景色
    self.view.backgroundColor = [UIColor whiteColor];

    //添加主视图
    self.mainPageView = [[MainPageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.mainPageView];
    [self.mainPageView.menuBtn addTarget:self action:@selector(openLeftList) forControlEvents:UIControlEventTouchUpInside];

    //添加侧边栏
    self.leftPageMenuView = [[LeftPageMenuView alloc] initWithFrame:CGRectMake( - kMenuViewWidth, 0, kMenuViewWidth, kScreenHeight)];
    [self.view addSubview:self.leftPageMenuView];
    
    
    [self.leftPageMenuView.loginBtn addTarget:self action:@selector(loginPush) forControlEvents:UIControlEventTouchUpInside];//登录跳转
    [self.leftPageMenuView.registerBtn addTarget:self action:@selector(registerPush) forControlEvents:UIControlEventTouchUpInside];//注册跳转
    [self.leftPageMenuView.headPhotoBtn addTarget:self action:@selector(personalCenterPush) forControlEvents:UIControlEventTouchUpInside];//个人中心
    [self.leftPageMenuView.ownerIndentBtn addTarget:self action:@selector(ownerIndentPush) forControlEvents:UIControlEventTouchUpInside];//车主订单跳转
    [self.leftPageMenuView.teantIndentBtn addTarget:self action:@selector(teantIndentPush) forControlEvents:UIControlEventTouchUpInside];//租客订单
    [self.leftPageMenuView.teantCertificationBtn addTarget:self action:@selector(teantCertificationPush) forControlEvents:UIControlEventTouchUpInside];//租客认证
    [self.leftPageMenuView.ownerCenterBtn addTarget:self action:@selector(ownerCenterPush) forControlEvents:UIControlEventTouchUpInside];//车主中心


    
    //滑动手势
    self.panMove = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.leftPageMenuView addGestureRecognizer:self.panMove];
    [self.panMove setCancelsTouchesInView:YES];
    self.panMove.delegate = self;
    
    //----------------------------时间选择器----------------------------------
    UIView *timeChoiceView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 50)];
    timeChoiceView.backgroundColor = [UIColor cyanColor];
    [self.mainPageView addSubview:timeChoiceView];
    
    //取车时间按钮
    self.checkOutCarTimeBtn = [DIYChoiceTimeButton buttonWithType:UIButtonTypeCustom];
    self.checkOutCarTimeBtn.frame = CGRectMake( 0, 0, kScreenWidth / 2, 50);
    self.checkOutCarTimeBtn.useCarTimeLabel.text = @"用车时间:";
    [self.checkOutCarTimeBtn addTarget:self action:@selector(checkOutCarTimePush) forControlEvents:UIControlEventTouchUpInside];
    [timeChoiceView addSubview:self.checkOutCarTimeBtn];
    
    //还车时间按钮
    self.giveBackCarTimeBtn = [DIYChoiceTimeButton buttonWithType:UIButtonTypeCustom];
    self.giveBackCarTimeBtn.userInteractionEnabled = NO;
    self.giveBackCarTimeBtn.frame = CGRectMake(kScreenWidth / 2, 0, kScreenWidth / 2, 50);
    self.giveBackCarTimeBtn.useCarTimeLabel.text = @"还车时间:";
    [self.giveBackCarTimeBtn addTarget:self action:@selector(giveBackCarTimePush) forControlEvents:UIControlEventTouchUpInside];
    [timeChoiceView addSubview:self.giveBackCarTimeBtn];
    
    
    //------------------------------此处以上-------------------------------------

    
    //汽车选择视图
    [self tableViewFengzhuang];
    

    //网络请求
    self.pageValue = 0;
    [self NetworkRequests];

    
    

}


#pragma mark 下拉刷新
- (void)headerWithRefreshing{
    
    
    self.choiceCarTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        if(self.pageValue > 0) {
            
            //当页数大于0 上拉刷新为 -1
            self.pageValue = self.pageValue - 1;
            
            // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                //网络解析
                [self NetworkRequests];
                
                //刷新数据
                [self.choiceCarTableView reloadData];
                
                //删除当前页
                [self.choiceCarTableView removeFromSuperview];

                // 结束刷新
                [self.choiceCarTableView.mj_header endRefreshing];
                
                //重新载入列表
                [self tableViewFengzhuang];
                
            });
        } else if (self.pageValue <= 0) {
            
            // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                //网络解析
                [self NetworkRequests];
                
                //刷新数据
                [self.choiceCarTableView reloadData];
                
                //删除当前页
                [self.choiceCarTableView removeFromSuperview];
                
                // 结束刷新
                [self.choiceCarTableView.mj_header endRefreshing];
                
                //重新载入列表
                [self tableViewFengzhuang];

                
            });
  
        }
        
    }];
}

#pragma mark 上拉刷新
- (void) footerWithRefreshing{
    
    
    // 上拉刷新
    self.choiceCarTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        if (self.pageValue < self.allPage - 1) {
            
            //上拉为当前页数加1
            self.pageValue = self.pageValue + 1;
            
            // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                //网络解析
                [self NetworkRequests];
                
                //刷新数据
                [self.choiceCarTableView reloadData];
                
                //删除当前页
                [self.choiceCarTableView removeFromSuperview];
                
                // 结束刷新
                [self.choiceCarTableView.mj_header endRefreshing];
                
                //重新载入列表
                [self tableViewFengzhuang];
                
            });

        } else if (self.pageValue >= self.allPage - 1){
            
            // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                //网络解析
                [self NetworkRequests];
                
                //刷新数据
                [self.choiceCarTableView reloadData];
                
                //删除当前页
                [self.choiceCarTableView removeFromSuperview];
                
                // 结束刷新
                [self.choiceCarTableView.mj_header endRefreshing];
                
                //重新载入列表
                [self tableViewFengzhuang];

                
            });

            
        }
        
    }];

}

#pragma mark tableView这辆列表封装
- (void)tableViewFengzhuang {
    //汽车选择视图
    self.choiceCarTableView = [[UITableView alloc] initWithFrame:CGRectMake( 0, 119, kScreenWidth, kScreenHeight - 114) style:UITableViewStylePlain];
    self.choiceCarTableView.dataSource = self;
    self.choiceCarTableView.delegate = self;
    self.choiceCarTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//去掉分割线
    [self.mainPageView addSubview:self.choiceCarTableView];
    
    [self.choiceCarTableView registerClass:[DIYChoiceCarTableViewCell class] forCellReuseIdentifier:reidentifier];
    
    [self headerWithRefreshing];
    
    [self footerWithRefreshing];
    
}



//==================================    网络请求   ===============================
#pragma mark 网络请求
- (void) NetworkRequests{
    
    //初始化进度框，置于当前的View当中
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.label.text = @"正在加载";
    [HUD showAnimated:YES];//HUD开始

    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    //传入的参数
    NSString *page = [NSString stringWithFormat:@"%ld", self.pageValue];
    
    NSString *chickOutTimeStampStr = [NSString stringWithFormat:@"%.0f", self.checkOutTimeStamp];
    NSString *giveBackTimeStampStr = [NSString stringWithFormat:@"%.0f", self.giveBackTimeStamp];
    self.d_ValueTimeStamp = self.giveBackTimeStamp - self.checkOutTimeStamp ;

    
    NSDictionary *para = [NSDictionary dictionary];
    
    
    if (self.d_ValueTimeStamp > 0) {
        para = @{
                 @"type" : @"1",
                 @"page" : page,
                 @"open_time" : chickOutTimeStampStr,
                 @"end_time" : giveBackTimeStampStr
                 };

    } else if (self.d_ValueTimeStamp <= 0) {

        para = @{
                 @"type" : @"1",
                 @"page" : page
                 };
    }


    //你的接口地址
    NSString *url = [NSString stringWithFormat:@"%@%@", kRealmName,kNetworkInterfaceCarList];
    //发送请求
    [manager POST:url parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.listArray = [NSMutableArray array];
        
        NSDictionary *dic = responseObject;
        
        [HUD hideAnimated:YES];

        self.status = dic[@"status"];
        
        self.msg = dic[@"msg"];

        NSDictionary *listDic = dic[@"info"];
        
            for (NSDictionary *diccc in listDic[@"list"]) {
                
                CarListMianPageModel *carListModel = [[CarListMianPageModel alloc] init];
                [carListModel setValuesForKeysWithDictionary:diccc];
                
                [self.listArray addObject:carListModel];
                                
            }
        
        NSString *pageAll = listDic[@"page_count"];
        
        self.allPage = [pageAll intValue];
        
        [self.choiceCarTableView reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [HUD hideAnimated:YES];
        
        //初始化提示框；
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有网络" preferredStyle:  UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
        }]];
        
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];
        
  
        
    }];
    
}
//---------------------------此处以上-----------------------------------------

//========================主页UItableViewDamo===================================
#pragma mark 主页UItableViewDamo
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.listArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return kScreenWidth * 221 / 375;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.cell = [tableView dequeueReusableCellWithIdentifier:reidentifier forIndexPath:indexPath];
    
    self.cell.model = self.listArray[indexPath.row];
    
    return self.cell;
    
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];//关闭选中状态
    
    self.cell.model = self.listArray[indexPath.row];
    self.d_ValueTimeStamp = self.giveBackTimeStamp - self.checkOutTimeStamp;
    
    if (self.d_ValueTimeStamp <= 0 || self.d_ValueTimeStamp > 7776000) {
        
        CarDetailsViewController *carDetails = [[CarDetailsViewController alloc] init];
        carDetails.carIDStr = self.cell.carId;
        [self.navigationController pushViewController:carDetails animated:nil];

    } else {
        
    CarDetailsViewController *carDetails = [[CarDetailsViewController alloc] init];
    
    carDetails.carIDStr = self.cell.carId;
    
    carDetails.checkOutCarTimeStr =  self.checkOutCarTime;
    
    carDetails.giveBackCarTimeStr = self.giveBackCarTime;
        
    [self.navigationController pushViewController:carDetails animated:nil];
   
    }
}

//=============================此处以上===========================================

//-----------------------时间选择器相关跳转---------------------------------------
#pragma mark 时间选择器弹出
//取车时间
- (void) checkOutCarTimePush {
    
    self.datePickerView = [[DIYDatePickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    [self.datePickerView didFinishSelectedDate:^(NSDate *selectDateTime) {
        
        if (selectDateTime) {
            
 
            if (self.giveBackCarTime == nil) {
                //如果取车时间为空
                
                self.checkOutCarTime = selectDateTime;
                
                self.checkOutTimeStamp = [self.checkOutCarTime timeIntervalSince1970];
                
                
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"MM-dd HH:mm"];
                
                NSString *strDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:selectDateTime]];
                
                self.checkOutCarTimeBtn.timeShowLabel.text = strDate;
                
                //更换时间图标
                self.checkOutCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-blue"];
                
                NSDateFormatter *dareFormatter1 = [[NSDateFormatter alloc] init];
                [dareFormatter1 setDateFormat:@"yyyyMMddHHmm"];
                
                self.giveBackCarTimeBtn.userInteractionEnabled = YES;
                

            } else {
                
                
                self.checkOutCarTime = selectDateTime;
                
                self.checkOutTimeStamp = [self.checkOutCarTime timeIntervalSince1970];
                
                self.d_ValueTimeStamp = self.giveBackTimeStamp - self.checkOutTimeStamp;
                
                if (self.d_ValueTimeStamp <= 0 ) {
                    //如果取车时间大于换车时间
                    //初始化提示框；
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"取车时间大于换车时间,还车时间清零" preferredStyle:  UIAlertControllerStyleAlert];

                    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  
                        
                        //点击按钮的响应事件；
                        self.giveBackCarTime = nil;
                        self.giveBackCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
                        
                        self.giveBackTimeStamp = [self.giveBackCarTime timeIntervalSince1970];
                        
                        self.checkOutCarTime = selectDateTime;
                        
                        self.checkOutTimeStamp = [self.checkOutCarTime timeIntervalSince1970];
                        
                        self.d_ValueTimeStamp = self.giveBackTimeStamp - self.checkOutTimeStamp;
                        
                        //更换时间图标
                        self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];

                        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
                        
                        NSString *strDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:selectDateTime]];
                        
                        self.checkOutCarTimeBtn.timeShowLabel.text = strDate;
                        
                        //更换时间图标
                        self.checkOutCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-blue"];
                        
                        NSDateFormatter *dareFormatter1 = [[NSDateFormatter alloc] init];
                        [dareFormatter1 setDateFormat:@"yyyyMMddHHmm"];
                        
                        self.giveBackCarTimeBtn.userInteractionEnabled = YES;
                    
                    }]];
                    
                    [self presentViewController:alert animated:true completion:nil];

                
                } else if (self.d_ValueTimeStamp > 7776000 ){
                    
                    //如果租车时间大于90天
                    //初始化提示框；
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"取车时间大于换车时间,还车时间清零" preferredStyle:  UIAlertControllerStyleAlert];
                    
                    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                        
                        //点击按钮的响应事件；
                        self.giveBackCarTime = nil;
                        self.giveBackCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
                        
                        self.giveBackTimeStamp = [self.giveBackCarTime timeIntervalSince1970];
                        
                        self.checkOutCarTime = selectDateTime;
                        
                        self.checkOutTimeStamp = [self.checkOutCarTime timeIntervalSince1970];
                        
                        self.d_ValueTimeStamp = self.giveBackTimeStamp - self.checkOutTimeStamp;
                        
                        //更换时间图标
                        self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];
                        
                        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
                        
                        NSString *strDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:selectDateTime]];
                        
                        self.checkOutCarTimeBtn.timeShowLabel.text = strDate;
                        
                        //更换时间图标
                        self.checkOutCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-blue"];
                        
                        NSDateFormatter *dareFormatter1 = [[NSDateFormatter alloc] init];
                        [dareFormatter1 setDateFormat:@"yyyyMMddHHmm"];
                        
                        self.giveBackCarTimeBtn.userInteractionEnabled = YES;
                        
                    }]];
                    
                    [self presentViewController:alert animated:true completion:nil];

                    
                } else {
                    
                    
                    
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat:@"MM-dd HH:mm"];
                    
                    NSString *strDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:selectDateTime]];
                    
                    self.checkOutCarTimeBtn.timeShowLabel.text = strDate;
                    
                    //更换时间图标
                    self.checkOutCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-blue"];
                    
                    NSDateFormatter *dareFormatter1 = [[NSDateFormatter alloc] init];
                    [dareFormatter1 setDateFormat:@"yyyyMMddHHmm"];
                    
                    self.giveBackCarTimeBtn.userInteractionEnabled = YES;
                    
                    
                }
                
            }
            
   

            
        } else {
            
            self.giveBackCarTimeBtn.userInteractionEnabled = NO;
//            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//            [dateFormatter setDateFormat:@"MM-dd HH:mm"];
//            NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
//            self.checkOutCarTimeBtn.timeShowLabel.text = strDate;
            self.checkOutCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
            self.checkOutCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];
            
            self.giveBackCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
            self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];

            self.checkOutCarTime = nil;
            self.giveBackCarTime = nil;
            self.checkOutTimeStamp = [self.checkOutCarTime timeIntervalSince1970];
            self.giveBackTimeStamp = [self.giveBackCarTime timeIntervalSince1970];
            self.d_ValueTimeStamp = self.giveBackTimeStamp - self.checkOutTimeStamp;
            
            //网络解析
            [self NetworkRequests];
            
            //刷新数据
            [self.choiceCarTableView reloadData];
            
            //删除当前页
            [self.choiceCarTableView removeFromSuperview];
            
            // 结束刷新
            [self.choiceCarTableView.mj_header endRefreshing];
            
            //重新载入列表
            [self tableViewFengzhuang];

            
        }
        
        
    }];
    
    
}

//还车时间
- (void) giveBackCarTimePush {
    
    self.giveBackDatePickerView = [[DIYGiveBackDatePickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    [self.giveBackDatePickerView didFinishSelectedDate:^(NSDate *selectDateTime) {
        
        if (selectDateTime) {
            
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"MM-dd HH:mm"];
            
            self.giveBackCarTime = selectDateTime;
            
            self.giveBackTimeStamp = [self.giveBackCarTime timeIntervalSince1970];
            
            self.d_ValueTimeStamp = self.giveBackTimeStamp - self.checkOutTimeStamp;
            
            if (self.d_ValueTimeStamp <= 0) {
                
                [self  timeDvalueAnalysis:1];

            } else if (self.d_ValueTimeStamp > 7776000) {
                
                [self  timeDvalueAnalysis:2];

            } else {
                
                
                //更换时间图标
                self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-blue"];
                
                //            NSDateFormatter *dareFormatter1 = [[NSDateFormatter alloc] init];
                //            [dareFormatter1 setDateFormat:@"yyyyMMddHHmm"];
                
                
                //网络解析
                [self NetworkRequests];
                
                //刷新数据
                [self.choiceCarTableView reloadData];
                
                //删除当前页
                [self.choiceCarTableView removeFromSuperview];
                
                // 结束刷新
                [self.choiceCarTableView.mj_header endRefreshing];
                
                //重新载入列表
                [self tableViewFengzhuang];
                
                NSString *strDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:selectDateTime]];
                
                self.giveBackCarTimeBtn.timeShowLabel.text = strDate;

                
            }
            
            

            
        } else {
            
            //            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            //            [dateFormatter setDateFormat:@"MM-dd HH:mm"];
            //            NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
            //            self.checkOutCarTimeBtn.timeShowLabel.text = strDate;
            self.giveBackCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
            
        }
    
    }];

}

//-----------------------------此处以上-----------------------------------------



#pragma mark 打开菜单栏的侧滑按钮
- (void) openLeftList {
        
    
    //将触摸手势添加到蒙板上
    self.tapClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchTap)];
    [self.contentView addGestureRecognizer:self.tapClick];

    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.leftPageMenuView.frame = CGRectMake( 0, 0, kMenuViewWidth, kScreenHeight);
        
        self.mainPageView.frame = CGRectMake(kMenuViewWidth, 0, kScreenWidth, kScreenHeight);
        
        [self.mainPageView addSubview:self.contentView];
        
    }];

}

#pragma mark 关闭菜单栏的侧滑按钮
- (void) closeLeftList {
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.leftPageMenuView.frame = CGRectMake( - kMenuViewWidth, 0, kMenuViewWidth, kScreenHeight);
        
        self.mainPageView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        
        
    } completion:^(BOOL finished) {
        
        [self.contentView removeFromSuperview];
        
    }];

}

#pragma mark 点击事件
- (void)touchTap{
    
    [self closeLeftList];
    
}


//滑动手势
- (void) handlePan: (UIPanGestureRecognizer *)rec{
    
    //获得移动坐标
    CGPoint point = [rec translationInView:self.view];
    
    CGFloat  x = fabs(point.x);//x方向位移
    CGFloat  y = fabs(point.y);//y方向位移
    self.scalef = point.x * vSpeedFloat;//位移
    
    //判断手指移动方向,向左滑动
    if (x > y && self.scalef < 0) {
        
        
        self.leftPageMenuView.frame = CGRectMake(self.scalef, 0, kScreenWidth - kMainPageDistance, kScreenHeight);
        
        self.mainPageView.frame = CGRectMake(kScreenWidth - kMainPageDistance + self.scalef, 0, kScreenWidth, kScreenHeight);
        
    }
    
    //手势结束后修正位置,超过约一半时向多出的一半偏移
    if (rec.state == UIGestureRecognizerStateEnded) {
        
        if (self.scalef * (-1) >= kScreenWidth / 5 && x > y) {
            
            [self closeLeftList];
            
        } else if (self.scalef * (-1) < kScreenWidth / 5 && self.scalef * (-1) >= 0 && x > y) {
            
            [self openLeftList];
            
        }
        
        self.scalef = 0;
    }
    
    
}


//跳转视图
- (void) loginPush{
    
    [self closeLeftList];
    
//    self.navigationController.navigationBarHidden = NO;
    
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    
    [self.navigationController pushViewController:loginVC animated:nil];
    
    
}

- (void) registerPush{
    
    [self closeLeftList];

    RegisterViewController *registVC = [[RegisterViewController alloc] init];
    
    [self.navigationController pushViewController:registVC animated:nil];
    
}

#pragma mark 个人中心跳转
- (void) personalCenterPush{
    
    [self closeLeftList];

    PersonalInformationDetailViewController *personalCenter = [[PersonalInformationDetailViewController alloc] init];
    
    [self.navigationController pushViewController:personalCenter animated:nil];

}

#pragma mark 车主订单跳转
- (void) ownerIndentPush {

    CarOwnerOrdersViewController *carOwnerOrderVC = [[CarOwnerOrdersViewController alloc] init];
    [self.navigationController pushViewController:carOwnerOrderVC animated:nil];

}
#pragma mark 租客订单
- (void) teantIndentPush {
    
    RenterOrdersViewController *renterOrderVC = [[RenterOrdersViewController alloc] init];
    [self.navigationController pushViewController:renterOrderVC animated:nil];
    
}
#pragma mark 租客认证
- (void) teantCertificationPush {

    TenantCertificationViewController *tenantCertification = [[TenantCertificationViewController alloc] init];
    
    [self.navigationController pushViewController:tenantCertification animated:nil];

}
#pragma mark 车主中心
- (void) ownerCenterPush {

    LongLetCarViewController *longLetCarVC = [[LongLetCarViewController alloc] init];
    [self.navigationController pushViewController:longLetCarVC animated:nil];
    
    
}

#pragma mark 伪造的导航条
- (void) openLeftListError{
    
    self.navigationController.navigationBarHidden = YES;

    //将触摸手势添加到蒙板上
    self.tapClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchTap)];
    [self.contentView addGestureRecognizer:self.tapClick];
    
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.leftPageMenuView.frame = CGRectMake( 0, 0, kMenuViewWidth, kScreenHeight);
        
        self.mainPageView.frame = CGRectMake(kMenuViewWidth, 0, kScreenWidth, kScreenHeight);
        
        [self.mainPageView addSubview:self.contentView];
        
    }];

}


#pragma mark 返回值分析
- (void) resultAnalysis {
    
    int status = [self.status intValue];
    
    switch (status) {
        case 99:{
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 98:{
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 97:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 96:
        {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 1:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 2:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 3:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 4:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 5:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 100:
        {

            
        }
            break;
            
        default:
            break;
    }
    
    
    
}

#pragma mark 时间控制分析
- (void) timeDvalueAnalysis:(NSInteger) status {
    
    
    
    switch (status) {
        case 1:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"还车时间必须大于用车时间" preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
                self.giveBackCarTime = nil;
                self.giveBackCarTimeBtn.timeShowLabel.text = @"-- / -- / --";

                //更换时间图标
                self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];

            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 2:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"最长租期不能大于90天" preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
                
                //点击按钮的响应事件；
                self.giveBackCarTime = nil;
                self.giveBackCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
                
                self.giveBackTimeStamp = [self.giveBackCarTime timeIntervalSince1970];
                //更换时间图标
                self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];

            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
            
        default:
            break;
    }
    

}

@end
